package com.dkd.manage.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.dkd.common.constant.DkdContants;
import com.dkd.manage.domain.VendingMachine;
import com.dkd.manage.service.IVendingMachineService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.dkd.common.annotation.Log;
import com.dkd.common.core.controller.BaseController;
import com.dkd.common.core.domain.AjaxResult;
import com.dkd.common.enums.BusinessType;
import com.dkd.manage.domain.Emp;
import com.dkd.manage.service.IEmpService;
import com.dkd.common.utils.poi.ExcelUtil;
import com.dkd.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 工单员工Controller
 *
 * @author yuwei
 * @date 2024-09-02
 */
@Api(tags = "工单员工管理接口")
@RestController
@RequestMapping("/manage/emp")
public class EmpController extends BaseController {

    @Autowired
    private IEmpService empService;

    @Autowired
    private IVendingMachineService vendingMachineService;

    /**
     * 查询工单员工列表
     */
    @PreAuthorize("@ss.hasPermi('manage:emp:list')")
    @ApiOperation(value = "获取工单员工列表", notes = "返回工单员工列表")
    @GetMapping("/list")
    public TableDataInfo list(Emp emp) {
        startPage();
        List<Emp> list = empService.selectEmpList(emp);
        return getDataTable(list);
    }

    /**
     * 根据设备编号查询设备区域下的运营人员
     */
    @PreAuthorize("@ss.hasPermi('manage:emp:list')")
    @ApiOperation(value = "根据设备编号查询设备区域下的运营人员", notes = "返回设备区域下的运营人员列表")
    @ApiParam(name = "innerCode", value = "设备编号", required = true)
    @GetMapping("/businessList/{innerCode}")
    public AjaxResult businessList(@PathVariable("innerCode") String innerCode) {
        VendingMachine vendingMachine = vendingMachineService.selectVendingMachineByInnerCode(innerCode);
        if (vendingMachine == null) {
            return error("售货机不存在");
        }
        Emp emp = new Emp();
        emp.setRegionId(vendingMachine.getRegionId());
        emp.setRoleCode(DkdContants.ROLE_CODE_BUSINESS); // 运营人员
        emp.setStatus(DkdContants.EMP_STATUS_NORMAL); // 启用状态
        return success(empService.selectEmpList(emp));
    }

    /**
     * 根据设备编号查询设备区域下的运维人员
     */
    @PreAuthorize("@ss.hasPermi('manage:emp:list')")
    @ApiOperation(value = "根据设备编号查询设备区域下的运维人员", notes = "返回设备区域下的运维人员列表")
    @ApiParam(name = "innerCode", value = "设备编号", required = true)
    @GetMapping("/operationList/{innerCode}")
    public AjaxResult operationList(@PathVariable("innerCode") String innerCode) {
        VendingMachine vendingMachine = vendingMachineService.selectVendingMachineByInnerCode(innerCode);
        if (vendingMachine == null) {
            return error("售货机不存在");
        }
        Emp emp = new Emp();
        emp.setRegionId(vendingMachine.getRegionId());
        emp.setRoleCode(DkdContants.ROLE_CODE_OPERATOR); // 运维人员
        emp.setStatus(DkdContants.EMP_STATUS_NORMAL); // 启用状态
        return success(empService.selectEmpList(emp));
    }

    /**
     * 导出工单员工列表
     */
    @PreAuthorize("@ss.hasPermi('manage:emp:export')")
    @ApiOperation(value = "导出工单员工列表", notes = "导出指定工单员工至Excel文件")
    @Log(title = "工单员工", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Emp emp) {
        List<Emp> list = empService.selectEmpList(emp);
        ExcelUtil<Emp> util = new ExcelUtil<>(Emp.class);
        util.exportExcel(response, list, "工单员工数据");
    }

    /**
     * 获取工单员工详细信息
     */
    @PreAuthorize("@ss.hasPermi('manage:emp:query')")
    @ApiOperation(value = "获取工单员工详细信息", notes = "根据ID获取工单员工详细信息")
    @ApiParam(name = "id", value = "工单员工ID", required = true)
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(empService.selectEmpById(id));
    }

    /**
     * 新增工单员工
     */
    @PreAuthorize("@ss.hasPermi('manage:emp:add')")
    @ApiOperation(value = "新增工单员工", notes = "创建新的工单员工")
    @Log(title = "工单员工", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Emp emp) {
        return toAjax(empService.insertEmp(emp));
    }

    /**
     * 修改工单员工
     */
    @PreAuthorize("@ss.hasPermi('manage:emp:edit')")
    @ApiOperation(value = "修改工单员工", notes = "根据ID修改工单员工")
    @Log(title = "工单员工", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Emp emp) {
        return toAjax(empService.updateEmp(emp));
    }

    /**
     * 删除工单员工
     */
    @PreAuthorize("@ss.hasPermi('manage:emp:remove')")
    @ApiOperation(value = "删除工单员工", notes = "根据ID删除工单员工")
    @ApiParam(name = "ids", value = "工单员工ID数组", required = true)
    @Log(title = "工单员工", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(empService.deleteEmpByIds(ids));
    }
}